package com.linkage.jz.bankruptcy.activity.Project;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.linkage.jz.bankruptcy.BankruptcyApp;
import com.linkage.jz.bankruptcy.Consts;
import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.activity.RegistLogin.AccountLoginActivity;
import com.linkage.jz.bankruptcy.bean.ManagerUserInfo;
import com.linkage.jz.bankruptcy.net.BaseSubscriber;
import com.linkage.jz.bankruptcy.net.Network;
import com.linkage.jz.bankruptcy.utils.SPUtils;
import com.linkage.jz.bankruptcy.widget.ShapeLoadingDialog;
import com.linkage.seapeak.base.BaseWhiteActivity;
import com.linkage.seapeak.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import java.io.IOException;
import java.util.Random;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


public class OtherUserPermissionActivity extends BaseWhiteActivity {
    private static final String TAG = AccountLoginActivity.class.getSimpleName();

    private ManagerUserInfo mInfo;
    private boolean canEdit = false;

    private CheckBox proXm;
    private CheckBox proJd;
    private CheckBox proZq;
    private CheckBox proBj;
    private CheckBox proTl;
    private CheckBox proRw;
    private CheckBox proGs;

    public static void start(Context context, ManagerUserInfo info, boolean canEdit) {
        Intent intent = new Intent(context, OtherUserPermissionActivity.class);
        intent.putExtra("info", new Gson().toJson(info));
        intent.putExtra("canEdit", canEdit);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_otheruser_permission, "权限管理");
        String bookJson = getIntent().getStringExtra("info");
        mInfo = new Gson().fromJson(bookJson, ManagerUserInfo.class);
        canEdit = getIntent().getBooleanExtra("canEdit", false);
        initView();
        checkEnable();
    }

    private void initView() {
        proXm = (CheckBox) findViewById(R.id.pro_xm);
        proJd = (CheckBox) findViewById(R.id.pro_jd);
        proZq = (CheckBox) findViewById(R.id.pro_zq);
        proBj = (CheckBox) findViewById(R.id.pro_bj);
        proTl = (CheckBox) findViewById(R.id.pro_tl);
        proRw = (CheckBox) findViewById(R.id.pro_rw);
        proGs = (CheckBox) findViewById(R.id.pro_gs);

        String column = mInfo.getProjectColumnPermission();
        proXm.setChecked(column.contains("0"));
        proJd.setChecked(column.contains("1"));
        proZq.setChecked(column.contains("2"));
        proBj.setChecked(column.contains("3"));
        proTl.setChecked(column.contains("4"));
        proRw.setChecked(column.contains("5"));
        proGs.setChecked(column.contains("6"));

        if (canEdit)
            addMenu(new MenuAction(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    saveData();
                }
            }, "完成", 0));
    }

    /**
     * 检查是否可编辑
     */
    void checkEnable() {
        proXm.setEnabled(canEdit);
        proJd.setEnabled(canEdit);
        proZq.setEnabled(canEdit);
        proBj.setEnabled(canEdit);
        proTl.setEnabled(canEdit);
        proRw.setEnabled(canEdit);
        proGs.setEnabled(canEdit);
    }

    public void saveData() {
        if (BankruptcyApp.tokeninfo == null || BankruptcyApp.tokeninfo.getToken() == null) {
            getToken(null);
            return;
        }

        String str = "";
        str += proXm.isChecked() ? ",0" : "";
        str += proJd.isChecked() ? ",1" : "";
        str += proZq.isChecked() ? ",2" : "";
        str += proBj.isChecked() ? ",3" : "";
        str += proTl.isChecked() ? ",4" : "";
        str += proRw.isChecked() ? ",5" : "";
        str += proGs.isChecked() ? ",6" : "";
        if (!TextUtils.isEmpty(str))
            str = str.substring(1);

        Network.getAPIService()
                .setOtherPermission(mInfo.getId(), str, getUserToken())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<JsonObject>() {
                    @Override
                    public void onNext(JsonObject json) {
                        int code = json.get("code").getAsInt();
                        if (code == 0) {
                            showToast("设置成功", ToastUtil.TOAST_TYPE.SUCCESS);
                            finish();
                        } else {
                            showToast("设置失败");
                        }
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        showToast("设置失败");
                    }
                });
    }
}
